package com.mindata.blockchain.socket.common.queue.base;

import com.mindata.blockchain.ApplicationContextProvider;
import com.mindata.blockchain.socket.DGL.queue.DGLCommitMsgQueue;
import com.mindata.blockchain.socket.DGL.queue.DGLPreMsgQueue;
import com.mindata.blockchain.socket.DGL.queue.DGLPrepareMsgQueue;
import com.mindata.blockchain.socket.common.queue.InstructionQueue;
import com.mindata.blockchain.socket.common.queue.ViewChangeQueue;
import com.mindata.blockchain.socket.common.msg.MsgType;
import com.mindata.blockchain.socket.common.msg.BaseMsg;
import com.mindata.blockchain.socket.pbft.queue.*;
import org.springframework.stereotype.Component;

/**
 * @author wuweifeng wrote on 2018/4/25.
 */
@Component
public class MsgQueueManager {

    public void pushMsg(BaseMsg msg) {
        BaseMsgQueue baseMsgQueue = null;
        switch (msg.getVoteType()) {
            case MsgType
                    .PREPREPARE:
                baseMsgQueue = ApplicationContextProvider.getBean(PreMsgQueue.class);
                break;
            case MsgType.PREPARE:
                baseMsgQueue = ApplicationContextProvider.getBean(PrepareMsgQueue.class);
                break;
            case MsgType.COMMIT:
                baseMsgQueue = ApplicationContextProvider.getBean(CommitMsgQueue.class);
                break;
            case MsgType.VIEWCHANGE:
                baseMsgQueue = ApplicationContextProvider.getBean(ViewChangeQueue.class);
                break;
            case MsgType.DLGPREPREPARE:
                baseMsgQueue = ApplicationContextProvider.getBean(DGLPreMsgQueue.class);
                break;
            case MsgType.DLGPREPARE:
                baseMsgQueue = ApplicationContextProvider.getBean(DGLPrepareMsgQueue.class);
                break;
            case MsgType.DLGCOMMIT:
                baseMsgQueue = ApplicationContextProvider.getBean(DGLCommitMsgQueue.class);
                break;
            case MsgType.INSTRUCTION:
                baseMsgQueue = ApplicationContextProvider.getBean(InstructionQueue.class);
                break;
            default:
                break;
        }
        if (baseMsgQueue != null) {
            baseMsgQueue.push(msg);
        }
    }
}
